<template>
  <div>
    <slot></slot>
  </div>
</template>
<script>
import { getList } from "network/mock.js";

// 引入 echarts 核心模块，核心模块提供了 echarts 使用必须要的接口。
import * as echarts from "echarts/core";
// 引入柱状图图表，图表后缀都为 Chart
import { BarChart, LineChart } from "echarts/charts";
// 引入提示框，标题，直角坐标系组件，组件后缀都为 Component
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  ToolboxComponent,
} from "echarts/components";
// 引入 Canvas 渲染器，注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from "echarts/renderers";

// 注册必须的组件
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  ToolboxComponent,
  BarChart,
  LineChart,
  CanvasRenderer,
]);
export default {
  props: ["bar"],
  data() {
    return {
      chart: null,
      barC: null,
      jobChart: null,

      dataArr: [],
    };
  },

  watch: {
    bar: function (newValue) {
      this.barC = newValue;
      newValue &&
        this.getBarChart(() => {
          this.init();
        });
    },
  },
  mounted() {},
  methods: {
    async getBarChart(callback) {
      let { list } = await getList();
      this.jobChart = list.find((r) => r.type === "Job");
      callback && callback();
    },
    init() {
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(this.barC);
      myChart.setOption({
        color: ["#2f89cf"],
        xAxis: {
          type: "category",
          data: this.jobChart.jobText,
          axisTick: {
            show: false,
            alignWithLabel: true,
          },
          // 修改文字的颜色
          axisLabel: {
            color: "rgba(255,255,255,.6)",
            fontSize: "10",
          },
          // 隐藏x轴的线
          axisLine: {
            show: false,
          },
        },
        yAxis: {
          type: "value",
          // 隐藏x轴的线
          // 修改文字的颜色
          axisTick: {
            show: false,
          },
          axisLabel: {
            color: "rgba(255,255,255,.6)",
            fontSize: "12",
          },
          axisLine: {
            lineStyle: {
              color: "rgba(255,255,255,.1)",
              // width: 1,
              // type: "solid"
            },
          },
          splitLine: {
            lineStyle: {
              color: "rgba(255,255,255,.1) ",
            },
          },
        },
        tooltip: {
          // 触发的方式
          trigger: "axis",
          axisPointer: {
            type: "shadow", //shadow  line
          },
        },
        // 修改图标的大小
        grid: {
          left: "10%",
          top: "10px",
          right: "3%",
          bottom: "10%",
          containLable: true,
        },
        series: [
          {
            data: this.jobChart.jobData,
            type: "bar",
            barWidth: "35%",
            itemStyle: {
              borderRadius: 5,
              opacity: 0.5,
            },
          },
        ],
      });
      // 设置图表的自动适应
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },
  },
};
</script>
<style scoped lang="scss"></style>
